package com.awspaas.user.apps.shhtaerospaceindustrial.event;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ValueListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class notifyBeVisitedUnitOfficeManager2 extends ValueListener {

	@Override
	public String execute(ProcessExecutionContext ctx) throws Exception {
		// TODO Auto-generated method stub
		if (ctx.getProcessElement().getName().equals("U1")) {
//	            info("流程全局跳转事件被触发-->" + ctx.getProcessInstance());
			StringBuffer userid = new StringBuffer("");
			String bindid = ctx.getProcessInstance().getId();
			String querySql = "select TARGETUNIT from BO_EU_INNERLOCATION_VISIT_APP where  bindid='" + bindid + "'";
			String appunit = objToStr(DBSql.getString(querySql, "TARGETUNIT"));

			String querySqlcar = "select  count(*) as isinpark  from BO_EU_INNLOC_VISIT_APP_CARS where  bindid='"
					+ bindid + "' and inoutdoor is not null";
			int isInHtPark = DBSql.getInt(querySqlcar, "isinpark");

			if (!"院部".equals(appunit)) {
//	            	String querySql2 = "select OFFICEMANAGER from BO_EU_SAST_UNIT_BWBM where OFFICEMANAGER is not null and  unit in (select TARGETUNIT from BO_EU_INNLOC_VISIT_APP_LOCS where bindid='"+bindid+"' )";
				String querySql2 = "select distinct OFFICEMANAGER from BO_EU_INNLOC_VISIT_APP_LOCS where OFFICEMANAGER is not null and  bindid='"
						+ bindid + "'";

				List<Map<String, Object>> dataList = DBSql.query(querySql2, new ColumnMapRowMapper(), new Object[] {});
				if (dataList != null && !dataList.isEmpty()) {
					for (Map<String, Object> dataMap : dataList) {
						String OFFICEMANAGER = CoreUtil.objToStr(dataMap.get("OFFICEMANAGER"));
						userid.append(OFFICEMANAGER).append(" ");
					}
				}
				if (isInHtPark > 0) {
					userid.append("gateadmin gateone gatetwo");
				}

				return userid.toString();
			} else {
				return null;
			}
		}
		return null;

	}

	public static String objToStr(Object obj) {
		return obj == null ? "" : obj.toString();
	}

}
